package com.cupk.controller;

import com.cupk.pojo.Book;
import com.cupk.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RequestMapping("/book")//请求路径，也就是说都通过book...来访问
@Controller//三重架构：Controller调用service，service再调用mapper
public class BookController {
    @Autowired(required = false)//自动完成一个值的传递
    private BookService bookService;

    /**
     * @description
     * @param model
     * @return java.lang.String
     * @throws
     * @author magicbookpro
     * @date 2022/6/22 17:33
     */


    @RequestMapping("/findall")
    public String findAllBooks(Model model){
        List<Book>bookList=bookService.findAllBooks();
        model.addAttribute("blist",bookList);
        return "book/booklist";
//        System.out.println(bookList.size());
//        for(Book book:bookList)
//            System.out.println(book.getName()+" "+book.getISBN()+" "+book.getPrice());
    }

    //向首页跳转
    @RequestMapping("/index")
    public String toIndex(){
        return"index";
    }

    //跳转到图书添加页面
    @RequestMapping("/toinsert")
    public String toInsert(){
        return "book/insertBook";
    }

    //添加图书
    @RequestMapping("/insert")
    public String insertBook(Book book,Model model){
        int i=bookService.insertBook(book);
        if(i>0){//图书添加成功，跳转到图书列表
            List<Book>bookList=bookService.findAllBooks();
            model.addAttribute("msg","添加图书成功！！");
            model.addAttribute("blist",bookList);
            return "book/booklist";
        }
        else {
            return "public/false";
        }
    }

    //删除图书
    @RequestMapping("/deletebyid/{id}")
    public String deleteBookById(@PathVariable("id")Integer id,Model model){
        int i=bookService.deleteBookById(id);
        if(i>0){//图书删除成功，跳转到图书列表
            List<Book>bookList=bookService.findAllBooks();
            model.addAttribute("msg","删除图书成功！！");
            model.addAttribute("blist",bookList);
            return "book/booklist";
        }
        else {
            return "public/false";
        }
    }

    //跳转到修改页面
    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id,Model model){
        Book book=bookService.findBookById(id);
        model.addAttribute("book",book);
        return "book/updateBook";
    }

    @RequestMapping("/update")
    public String upDateBook(Book book,Model model){
        int i=bookService.updateBook(book);
        if(i>0){
            List<Book>bookList=bookService.findAllBooks();
            model.addAttribute("msg","图书信息修改成功！");
            model.addAttribute("blist",bookList);
            return "book/booklist";
        }
        else {
            return "public/false";
        }
    }

    //根据图书编号查询图书
    @GetMapping("/findbyid")
    public Book findBookById(@RequestParam(name="bookid")Integer id){
        System.out.println(bookService.findBookById(id));
        return bookService.findBookById(id);
    }

    //根据字符串模糊查询图书
    @RequestMapping("/findbystr")
    public String findBookByStr(String searchStr,Model model){
        List<Book>bookList=bookService.findBookByStr(searchStr);
        model.addAttribute("blist",bookList);
        return "book/booklist";
    }

    //批量删除
    @RequestMapping("/delete")
    public String deleteBooks(Integer[] bookids,Model model){
        if(bookids!=null) {
            int i = bookService.deleteBooks(bookids);
            if (i > 0) {
                List<Book>bookList=bookService.findAllBooks();
                model.addAttribute("msg","图书批量删除成功！");
                model.addAttribute("blist",bookList);
                return "book/booklist";
            }
            else {
                return "public/false";
            }
        }
        else{
            model.addAttribute("msg","未选择任何图书！");
            return "public/false";
        }
    }

}
